import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
 
def send_email(smtp_server, port, sender_email, receiver_email, password, message):
    msg = MIMEMultipart()
    msg['From'] = sender_email
    msg['To'] = receiver_email
    msg['Subject'] = Header('邮件主题', 'utf-8')
 
    body = MIMEText(message, 'plain', 'utf-8')
    msg.attach(body)
 
    try:
        if smtp_server == 'smtp.gmail.com':
            server = smtplib.SMTP_SSL(smtp_server, port)  # 使用 SSL/TLS 连接
        else:
            server = smtplib.SMTP(smtp_server, port)  # 对于非 Gmail，可能需要 STARTTLS
            server.starttls()  # 启动 TLS 加密
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, msg.as_string())
        server.quit()
        print("邮件发送成功")
    except smtplib.SMTPException as e:
        print(f"发送失败: {e}")
 
# 使用 Gmail SMTP 服务器发送邮件的示例
smtp_server = 'smtp.139.com'
port = 465  # 使用 SSL 时端口通常是 465
sender_email = 'pc_cloudsync@139.com'
receiver_email = 'chenkangfu139@139.com'
password = 'chenkf@123'
message = '这是邮件的正文内容。'
 
send_email(smtp_server, port, sender_email, receiver_email, password, message)